package com.itheima.reggie.service.impl;

import com.itheima.reggie.dao.AddressBookDao;
import com.itheima.reggie.exception.CustomException;
import com.itheima.reggie.pojo.AddressBook;
import com.itheima.reggie.service.AddressBookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AddressBookServiceImpl implements AddressBookService {
    @Autowired
    private AddressBookDao addressBookDao;

    @Override
    public void save(AddressBook addressBook) {
        addressBookDao.save(addressBook);
    }

    @Override
    public List<AddressBook> queryAddressList(Long userId) {
        return addressBookDao.queryAddressList(userId);
    }

    @Override
    public void updateDefaultAddress(AddressBook addressBook) {
        addressBookDao.removeDefaultAddress(addressBook.getUserId());
        addressBookDao.updateDefaultAddress(addressBook);
    }

    @Override
    public void updateAddress(AddressBook addressBook) {
        addressBookDao.updateAddress(addressBook);
    }

    @Override
    public AddressBook getById(Long id) {
        return addressBookDao.getById(id);
    }

    @Override
    public void deleteById(Long id) {
        addressBookDao.deleteById(id);
    }

    @Override
    public AddressBook getDefaultAddress(Long userId) {
        AddressBook defaultAddress = addressBookDao.getDefaultAddress(userId);
        if (defaultAddress==null) {
            throw new CustomException("用户没有设置默认地址");
        }
        return defaultAddress;
    }
}
